<?php

namespace app\admin\controller;


use app\models\CampusModel;
use app\models\CompanyModel;
use app\models\GoodsModel;
use app\models\HolidayModel;
use app\models\RegionalModel;
use app\models\UserModel;

class Goods extends Common
{

    /**
     * 用户管理模型.
     *
     *  @var UserModel
     */
    protected $userModel;


    /**
     * 用户管理模型.
     *
     *  @var GoodsModel
     */
    protected $goodsModel;


    /**
     * 区域管理模型.
     *
     *  @var RegionalModel
     */
    protected $regionalModel;


    /**
     * 校区管理模型.
     *
     * @var CampusModel
     */
    protected $campusModel;

	/**
	 * 集团管理模型.
	 *
	 * @var CompanyModel
	 */
	protected $companyModel;

    /**
     * 物品管理管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $result = $this->goodsModel->getGoodsLists($param);
            $this->success('success', '', $result, 0);
        }

		return $this->fetch('', [
            'type'=>get_current_type(),
            'company_id'=>get_current_company_id()
        ]);
    }

    /**
     * 添加物品
     * modify by Small Y
     *
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goods_add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $data_['company_id'] = $data['company_id'];
            $data_['campus_id'] = $data['campus_id'];
            $data_['goods_name'] = $data['goods_name'];
            $data_['unit'] = $data['unit'];
            $data_['price'] = $data['price'] * 100;
            $data_['cost_price'] = $data['cost_price'] * 100;
            if($data['type'] == 1){
                $data_['put_nums'] = 100000;
                $data_['residue_num'] = 100000;
            }else if($data['type'] == 0){
                $data_['put_nums'] = $data['put_nums'];
                $data_['residue_num'] = $data_['put_nums'];
            }
            $data_['type'] = $data['type'];

            if($this->goodsModel->get(['campus_id'  => $data_['campus_id'], 'company_id' =>  $data_['company_id'], 'goods_name' => $data_['goods_name']])){
                $this->error('该物品已经存在', null, '', 1);
            }

            if(!$this->goodsModel->save($data_)){
                $this->error('添加物品失败', null, '', 1);
            }
            $this->success('添加物品成功', url('Goods/lists'), '', 1);
        }

        $where = [];
        if(get_current_type() == 4 || get_current_type() == 5){
            $where['id'] = get_current_campus_id();
        }elseif (get_current_type() == 2 || get_current_type() == 3){
            $where['id'] = ['in',get_current_campus_id()];
        }
        if(get_current_type() == 1){
            $company_id = input('company_id');
        }else{
            $company_id = get_current_company_id();
        }
        $where['company_id'] = $company_id; //集团ID


        /***********区域**********/
        $regional_result = $this->regionalModel->select();
        $regional_arr =[];
        foreach ($regional_result as $key => $value){
            $regional_arr[$value['id'].''] = $value['regional_name'];
        }
        /*****************************/

        $campus = $this->campusModel->where($where)->select();  //校区

        if(get_current_type() == 1){  //超级管理员
            foreach ($campus as $key => $value) {
                $value['campus_name'] = $value['campus_name'] . '【' . $regional_arr[$value['regional_id'] . ''] . '】';
            }
        }

        return $this->fetch('',['campus' => $campus, 'company_id' => $company_id]);
    }

    /*
    *修改物品类型
     */
    public function change_type(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (!$this->goodsModel->where(['id' => $data['id']])->update(['type' => $data['type']])) {
                $this->error('修改物品类型失败', null, '', 1);
            }
            $this->success('修改物品类型成功', url('Goods/lists'), '', 1);
        }
        return $this->fetch('',['id' => input('id')]);
    }

    /**
     * 编辑物品属性
     *
     * @return mixed
     */
    public function goods_edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if($data['status_type'] == 1){  //put_nums
                $data_['put_nums'] = $data['put_nums'];
                $res = $this->goodsModel->get(['id' => $data['id']]);
                $out_nums = $res['out_nums'];
                $data_['residue_num'] = $data_['put_nums'] - $out_nums;  //剩余库存
                if($res['type'] == 1){
                    $this->success('杂费类型不支持编辑库存数量', url('Goods/lists'), '', 1);
                }
            }else if($data['status_type'] == 3){  //cost_price
                $res = $this->goodsModel->get(['id' => $data['id']]);
                $data_['cost_price'] = $data['cost_price'];
                if($res['type'] == 1){
                    $this->success('杂费类型不支持编辑成本价格', url('Goods/lists'), '', 1);
                }
            }else{
                $data_ = $data;
                unset($data_['status_type']);
            }
            if (!$this->goodsModel->save($data_, ['id' => $data['id']])) {
                $this->error('编辑物品属性失败', null, '', 1);
            }
            $this->success('编辑物品属性成功', url('Goods/lists'), '', 1);
        }
    }


    /**
     * 更改物品状态.
     *
     * @return mixed
     */
    public function goods_status()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (1 == $data['status']) {
                $msg = '启用';
            } else {
                $msg = '禁用';
            }
            if (!$this->goodsModel->save($data, ['id' => $data['id']])) {
                $this->error($msg.'物品失败', null, '', 1);
            }
            $this->success($msg.'成功', '', '', 1);
        }
    }

    /**
     * 删除物品
     *
     * @return mixed
     */
    public function goods_del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            if (!$this->goodsModel->where(['id' => input('id')])->delete()) {
                $this->error('删除物品失败', null, '', 1);
            }
            $this->success('删除物品成功', url('Goods/lists'), '', 1);
        }
    }


    /**
     * 选择集团
     * modify by Small Y
     *
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function company()
    {
        if ($this->request->isAjax()) {
            $company_id = input('company_id');
            $this->success('success', url('Goods/goods_add', ['company_id' => $company_id]), '', 0);
        }
        $where = [];
        $where['status'] = 1;

        /*************过滤掉未分配系统账号的集团*************/
        $user_result = $this->userModel->where(['wtype' => 2])->select();
        $company_user_arr = [];
        foreach ($user_result as $key => $value){
            $company_user_arr[] = $value['company_id'];
        }
        $company_user_arr = array_unique($company_user_arr);
        $company_user_str = implode(',',$company_user_arr);
        $where['id'] = ['in',$company_user_str];

        $company = $this->companyModel->where($where)->select();

        return $this->fetch('', [
            'company' => $company
        ]);
    }

    /**
     *  实例化.
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->goodsModel = new GoodsModel();
        $this->companyModel = new CompanyModel();
        $this->userModel = new UserModel();
        $this->campusModel = new CampusModel();
        $this->regionalModel = new RegionalModel();

    }
}
